package com.ishadows.notebook;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.app.library.activity.BaseActivity;
import com.app.library.annotation.InjectView;
import com.ishadows.notebook.dialog.CustomTextInput;

public class NoteDirActivity extends BaseActivity {
	@InjectView(ViewId=R.id.note_dir)
	private ListView dirListView;
	private List<String> datas;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		injectView();
		initDir();
	}

	public void addDir(View v){
		final CustomTextInput text =new CustomTextInput(this);
		text.setTitle("输入文件夹的名字");
		text.setSureClick(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(!TextUtils.isEmpty(text.getText())){
					if(datas.contains(text.getText().toString())){
						return ;
					}
					datas.add(text.getText().toString());
					getEditor().putString("dirList", JSON.toJSONString(datas)).commit();
					ArrayAdapter<String> array =new ArrayAdapter<String>(NoteDirActivity.this, android.R.layout.simple_list_item_1, datas);
					dirListView.setAdapter(array);
					text.dismiss();
				}
			}
		});
		text.setCancelClick(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				text.dismiss();
			}
		});
		text.show();
	}
	
	public void initDir(){
		String dirList =getShare().getString("dirList", null);
		if(dirList!=null){
			datas=JSON.parseArray(dirList, String.class);
		}else {
			datas=new ArrayList<String>();
		}
		ArrayAdapter<String> array =new ArrayAdapter<String>(this, R.layout.text_item, datas);
		dirListView.setAdapter(array);
		dirListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent i =new Intent(NoteDirActivity.this,NoteListActivity.class);
				i.putExtra("dirName", datas.get(position));
				startActivity(i);
			}
		});
	}

}
